﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WuDada.Core.Persistence.Ado;
using WuDada.Core.Persistence;
using WuDada.Core.SystemApplications.Domain;
using NHibernate.Criterion;

namespace WuDada.Core.SystemApplications.Persistence
{
    public class CityDao : AdoDao, ICityDao
    {
        public INHibernateDao NHibernateDao { get; set; }

        /// <summary>
        /// 新增縣市
        /// </summary>
        /// <param name="cityVO">被新增的縣市</param>
        /// <returns>新增後的縣市</returns>
        public CityVO CreateCity(CityVO cityVO)
        {
            NHibernateDao.Insert(cityVO);

            return cityVO;
        }

        /// <summary>
        /// 更新縣市
        /// </summary>
        /// <param name="cityVO">被更新的縣市</param>
        /// <returns>更新後的縣市</returns>
        public CityVO UpdateCity(CityVO cityVO)
        {
            NHibernateDao.Update(cityVO);

            return cityVO;
        }

        /// <summary>
        /// 取得縣市 By 識別碼
        /// </summary>
        /// <param name="cityId">識別碼</param>
        /// <returns>縣市</returns>
        public CityVO GetCityById(int cityId)
        {
            return NHibernateDao.GetVOById<CityVO>(cityId);
        }

        /// <summary>
        /// 取得縣市清單
        /// </summary>
        /// <param name="type">縣市類別</param>
        /// <returns>縣市清單</returns>
        public IList<CityVO> GetCityList()
        {
            DetachedCriteria dCriteria = DetachedCriteria.For<CityVO>();

            int count = NHibernateDao.CountByDetachedCriteria(dCriteria);

            if (count == 0)
            {
                return null;
            }

            return NHibernateDao.SearchByDetachedCriteria<CityVO>(dCriteria);
        }

        /// <summary>
        /// 取得縣市 By 縣市名稱
        /// </summary>
        /// <param name="cityName">縣市名稱</param>
        /// <returns>縣市</returns>
        public CityVO GetCityByCityName(string cityName)
        {
            DetachedCriteria dCriteria = DetachedCriteria.For<CityVO>();
            dCriteria.Add(Expression.Eq("CityName", cityName));

            int count = NHibernateDao.CountByDetachedCriteria(dCriteria);

            if (count == 0)
            {
                return null;
            }

            return NHibernateDao.SearchByDetachedCriteria<CityVO>(dCriteria, 0, 1)[0];
        }

        /// <summary>
        /// 新增地區
        /// </summary>
        /// <param name="areaVO">被新增的地區</param>
        /// <returns>新增後的地區</returns>
        public AreaVO CreateArea(AreaVO areaVO)
        {
            NHibernateDao.Insert(areaVO);

            return areaVO;
        }

        /// <summary>
        /// 更新地區
        /// </summary>
        /// <param name="areaVO">被更新的地區</param>
        /// <returns>更新後的地區</returns>
        public AreaVO UpdateArea(AreaVO areaVO)
        {
            NHibernateDao.Update(areaVO);

            return areaVO;
        }

        /// <summary>
        /// 取得地區 By 識別碼
        /// </summary>
        /// <param name="areaId">識別碼</param>
        /// <returns>地區</returns>
        public AreaVO GetAreaById(int areaId)
        {
            return NHibernateDao.GetVOById<AreaVO>(areaId);
        }

        /// <summary>
        /// 取得地區清單
        /// </summary>
        /// <returns>地區清單</returns>
        public IList<AreaVO> GetAreaList()
        {
            DetachedCriteria dCriteria = DetachedCriteria.For<AreaVO>();

            int count = NHibernateDao.CountByDetachedCriteria(dCriteria);

            if (count == 0)
            {
                return null;
            }

            return NHibernateDao.SearchByDetachedCriteria<AreaVO>(dCriteria);
        }

        /// <summary>
        /// 取得地區 By 地區名稱 And CityId
        /// </summary>
        /// <param name="areaName">地區名稱</param>
        /// <param name="cityId">CityId</param>
        /// <returns>地區</returns>
        public AreaVO GetAreaByAreaNameAndCityId(string areaName, int cityId)
        {
            DetachedCriteria dCriteria = DetachedCriteria.For<AreaVO>();
            dCriteria.Add(Expression.Eq("AreaName", areaName));
            dCriteria.CreateCriteria("City").Add(Expression.Eq("CityId", cityId));

            int count = NHibernateDao.CountByDetachedCriteria(dCriteria);

            if (count == 0)
            {
                return null;
            }

            return NHibernateDao.SearchByDetachedCriteria<AreaVO>(dCriteria, 0, 1)[0];
        }

        /// <summary>
        /// 取得地區清單 By CityId
        /// </summary>
        /// <param name="cityId">CityId</param>
        /// <returns>地區清單</returns>
        public IList<AreaVO> GetAreaListByCityId(int cityId)
        {
            DetachedCriteria dCriteria = DetachedCriteria.For<AreaVO>();
            dCriteria.CreateCriteria("City").Add(Expression.Eq("CityId", cityId));

            int count = NHibernateDao.CountByDetachedCriteria(dCriteria);

            if (count == 0)
            {
                return null;
            }

            return NHibernateDao.SearchByDetachedCriteria<AreaVO>(dCriteria);
        }
    }
}
